Skip to content

fix: allow UNIQUE INDEX in CREATE TABLE - #2457

Open
fudianchn wants to merge 1 commit into
JSQLParser:masterfrom
fudianchn:feat/unique-index-create-table-1893
Open

fix: allow UNIQUE INDEX in CREATE TABLE#2457
fudianchn wants to merge 1 commit into
JSQLParser:masterfrom
fudianchn:feat/unique-index-create-table-1893

Conversation

@fudianchn

Copy link
Copy Markdown
Contributor

What

Allow an optional UNIQUE prefix on INDEX inside CREATE TABLE, so MySQL-style UNIQUE INDEX name (cols) ... parses and round-trips.

Why

MySQL accepts UNIQUE INDEX name (cols) ... as an inline table constraint (it is produced by tooling and appears in real schemas). JSQLParser currently rejects it:

net.sf.jsqlparser.JSQLParserException: Encountered unexpected token: "UNIQUE" "UNIQUE"

Only INDEX name (...) and [UNIQUE] KEY name (...) were accepted; the UNIQUE INDEX combination fell through every CreateTableConstraint alternative.

How

The INDEX branch of CreateTableConstraint now accepts an optional leading UNIQUE, and the index type is rendered as UNIQUE INDEX when present (mirroring the existing [UNIQUE] KEY branch). LOOKAHEAD(3) becomes LOOKAHEAD(4) to cover the extra token. The branches remain unambiguous: UNIQUE INDEX name ( matches this branch, UNIQUE KEY name ( still matches the [UNIQUE] ... KEY branch, and UNIQUE ( still matches the [CONSTRAINT] UNIQUE branch.

Testing

  • New CreateTableTest#testUniqueIndexIssue1893UNIQUE INDEX idx (a, b) USING BTREE COMMENT '...' round-trips (fails before the grammar change), and a plain INDEX idx (a) still parses.
  • ./gradlew test --tests net.sf.jsqlparser.statement.create.CreateTableTest — green.
  • Full ./gradlew test — no regressions introduced by this change. The 33 unrelated failures observed locally are pre-existing environmental issues (Mockito inline MockMaker does not initialise under the local JDK 17 / Windows toolchain, and ParserKeywordsUtilsTest resolves its temp file to C:\WINDOWS); both reproduce on unmodified master.

Fixes #1893

Accept an optional UNIQUE prefix on the INDEX branch of CreateTableConstraint, so MySQL-style CREATE TABLE ... UNIQUE INDEX name (cols) ... parses and round-trips. UNIQUE INDEX is disambiguated from UNIQUE KEY and the bare UNIQUE constraint by the LOOKAHEAD.

Fixes JSQLParser#1893

Signed-off-by: 付典 <fudianchn@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Caused by: net.sf.jsqlparser.parser.ParseException: Encountered unexpected token: "UNIQUE" "UNIQUE"

1 participant